#include "threadPool.h"

ThreadPool::ThreadPool()
{
    pthread_mutex_init(&thread_mutex, nullptr);
    pthread_cond_init(&thread_condition, nullptr);\
    is_runing = true;
}

ThreadPool::~ThreadPool()
{}

void ThreadPool::create_thread_pool(int thread_num)
{
    for (int idx = 0; idx < thread_num; idx++) {
        pthread_t task_id = 0;
        pthread_create(&task_id, nullptr, thread_func, this);
        thread_list.push_back(task_id);
    }
}

void ThreadPool::addTask(Task& task)
{
    pthread_mutex_lock(&thread_mutex);
    task_queue.push(task);
    pthread_mutex_unlock(&thread_mutex);
    // 释放条件变量，告诉线程池有任务过来
    pthread_cond_signal(&thread_condition);
}

void* ThreadPool::thread_func(void* arg)
{
    // 每一个线程处理函数都尝试到任务队列中获取任务，去执行
    ThreadPool* pool = static_cast<ThreadPool*>(arg);
    while(pool->is_runing) {
        Task* task = pool->get_task_2_execute();
        if (!task) {

        }
    }
    return nullptr;
}

Task* ThreadPool::get_task_2_execute()
{

}

Task::Task()
{}

Task::~Task()
{}

void Task::setArgs(string str)
{}